Renaissance translator. Huzzah!

Table of Contents

This is a “pair” assignment, which means that if you are working on a team with someone else, you and your partner should do your best to engage in the pair programming model. At any point in time, one of you is “driving,” i.e. actually using the keyboard and mouse. The other is actively engaged following along, preventing bugs, and providing ideas.

You should make sure that over the course of an assignment that you spend roughly the same amount of time each “driving.” I will also ask you to turn in a form rating the work that your partner does. My recommendation is to take turns approximately every 15 minutes or so. Set a timer to help you remember.

1. Overview

An important and common use for computer science ideas is for translating or converting text from one form, or one language, to another. Tools like Google Translate will convert text from one language to another. For this assignment, you’ll create a computer translator that will take text and convert it to Renaissance speech, a form of speech used at Renaissance fairs (or perhaps I should say Renaissance Faires) that may or may not be based in historical reality.

2. Get started

Create a folder in which to store your work for this assignment.

  • If you are working on your own computer, it’s up to you where to put the folder. Your desktop is likely as good a place as any. Make a folder titled renaissance.
  • If you are working in the labs in Olin, make sure to first mount the COURSES folder, so that you won’t lose your code when you log out. Once you’ve done so, open up Finder, then navigate to your personal student work folder. You can then make a renaissance folder within there.
  • Once you’ve done so, you should then open up your new folder in VS Code. To do so, start up VS Code, then drag your folder onto the VS Code window. This should open up the folder within VS Code. If you are asked, click that you trust the authors.

3. Specifics

Your mission is to create a program to convert English to Renaissance. Specifically, here are some rules to follow.

3.1. Translation

Replace words as follows:

  • hello -> well met
  • goodbye -> fare thee well
  • please -> prithee
  • restroom -> privy
  • here -> hither
  • maybe -> perchance

Feel free to add more if you like, but do that later as that’s easy once you’ve got the above working. You might want to get this working with just a few words, do the rest of the assignment, then come back to finish this for fun.

Bear in mind that the above list contains the words that are changed when we translate from English to Renaissance. When I use the word translate, it applies to all words, even those that aren’t changed. For example, the phrase “Hello friend” translates to “Well met friend”. The words “Hello” and “friend” are both translated from English to Renaissance. Only the word “Hello” is changed as part of the translation.

3.2. Capitalization

If a word begins with a capital letter, then when you translate it, it should also begin with a capital letter. Ignore the case of the rest of the word. In other words, translate “Hello” / “HELLO” / “HeLlO” / “HeLLO” / etc. as “Well met”, but translate “hello” / “hELLO” / “hElLo” / “hEllo” / etc. as “well met”. You should only output “Well met” or “well met” as a translation for any form of “hello” – none of the other letters in “Well met” or “well met” should ever be capitalized apart from possibly the first. Similarly, the word “FrienD” should translate as “Friend”. (You should definitively not do this by testing individually for all possible ways the word “HeLlO” might appear. You’ll have over 200 cases for “restroom” if you do it that way.)

3.3. Huzzah!

Renaissance people are very enthusiastic, and have a habit of saying “Huzzah!” a lot. Get your translator to randomly insert “Huzzah!” between sentences. Anytime you encounter the end of a sentence (as denoted by a period, question mark, or exclamation point), decide with a 50/50 chance whether to insert a “Huzzah!”

4. Breaking up the words

Separating the words from the punctuation is kind of tricky in Python. Python makes it easy when things you want to read are separated by spaces, not punctuation. Therefore, your input should have spaces between anything you want Python to read in separately, such as:

Hello ! Please , could you maybe tell me where the restroom is ?

Your program should allow you to type in your input, and print out to the terminal window the translated version of your text. Just to get you started, the below sample code reads a phrase that you enter and then prints out space-delimited words one-by-one, so that in principle you could do something with each word if you wished. Feel free to take a look and use this if it is helpful, but you may want to type in yourself any code that you use (copying and pasting with the mouse tends to make you skip actually learning it, whereas retyping it seems to get it to sink in better.)

phrase = input()

for word in phrase.split():
    print(word, end = ' ')

print()

5. Redirecting the input from a file

While testing your code, you will likely find that it gets cumbersome to keep retyping your input over and over. It will be much faster if you put your input in a file, and redirect the input to your program to come from that file. In other words, create a file called source.txt (for example) that contains your input text, such as the example in the previous section. Then when you run your program, redirect your input to come from that file.

Redirecting your input from a file differs depending on whether you’re using a Mac or Windows. On a Mac, run your program like this:

$ python renaissance.py < source.txt

Think of the < as an arrow, directing the text inside source.txt to be the input to your program.

Alternatively, on Windows, if you are running in the Windows PowerShell prompt (this should be the default in VSCode), the following should work:

cat source.txt | python3 renaissance.py

(You might need to say `python` instead of `python3`, depending on how it installed.)

(As it turns out, this Windows PowerShell approach also works on Macs.)

6. Exemplary grade

If you get this far, you should feel proud of your achievements! If you want to push yourself harder and go for the exemplary grade, do the following extra challenge.

  • Get your program to be able to successfully change the English phrase “thank you” to the Renaissance phrase “grammercy”. It should handle capitalization reasonably as for the rest of the assignment. However, if the word “thank” appears on its own without the word “you” immediately following it, the word “thank” should remain unchanged. We will not test the particularly complicated case of “thank thank you”, so your program can handle that situation however you like.)

7. Optional bonus: dealing with the punctuation better

This section is completely optional. If you want to, feel free to try to get your program working without needing to put spaces between words and punctuation. The below sample program is much like the input sample provided above, but separates words whenever the text transitions from a “word character” (i.e., a letter) to a “non-word character”, or vice-versa. You’re not responsible for knowing how the weird delimiter stuff works in this program, though I’m happy to explain it if anyone asks.

from re import *
from sys import *

phrase = input()

regex = compile('\w+|\W+')
for word in regex.findall(phrase):
    print(word, end="")

print()

8. Test your code

Here are input files you can try to see how your code is doing, along with correct output. Note that some of those files are for the exemplary portion only.

9. Style

You should make sure that your program follows good style. You should it as readable as possible for someone else trying to understand them. At a minimum, you should put a brief comment at the top explaining what the program does, and all of your variable names should be meaningful.

10. Grading

You will receive an M for this assignment if…

  • your program displays the right output for each “M” input file. This means that the output matches the output I have provided, except for the fact that you will additionally have “Huzzah!” randomly appearing between sentences. When running the program multiple times, “Huzzah!” behavior changes.
  • your program is written to work in general, and not to only work for the specific tests that we have provided.
  • the 26 letters of the alphabet don’t explicitly appear in your code in some fashion. As an example, you don’t have code that looks something like

      if letter=='a' or letter=='b' or ...
    
  • each word that needs translating is only compared once in your code. As an example, you don’t have code that looks something like

      if x == "hello" or x == "Hello" ...
    

You will receive a grade of E for this assignment if you satisfy the above M requirements, and …

  • you satisfy the requirements for M
  • your program has a Python comment at the top with at least 5 words describing what the program does.
  • every one of your variable names is meaningful in some way. (Names such as thing, number, etc. are not meaningful.)
  • your code displays the right output for each “E” input file.

11. Submit your work

When finished, zip up your code and submit your work through Moodle.

Good luck, and have fun! Remember that lab assistants are available to help out if you need it, and you can attend prefect sessions as well.

Author: Dave Musicant